<?php


namespace common\behavior;


use common\model\NoteModel;
use common\model\NoteRecordModel;

/**
 * 需求变更记录
 */
class NoteCommentUpdateBehavior
{
    public function run($params)
    {
        try {
            if (empty($params['record']['data']['old'])) {
                return;
            }
            $noteId = $params['record']['primary_id'];
            $noteData = model(NoteModel::class)->where(['id' => $noteId])->field('text')->find();
            $oldRecordData = $params['record']['data']['old'];
            if (empty($oldRecordData['text'])) {
                $data['update_text'] = $noteData['text'] ?? '';
            }
            if (!empty($oldRecordData['text'])) {
                $data['update_text'] = $oldRecordData['text'];
            }
            if (!empty($oldRecordData['json'])) {
                $data['update_json'] = $oldRecordData['json'];
            }
            $data['note_id'] = $params['record']['primary_id'];
            model(NoteRecordModel::class)->addItem($data);
        } catch (\Exception $e) {
            trace('需求变更记录失败' . PHP_EOL . format_exception_info($e), 'ERR');
        }
    }
}
